package Hashmap;

import java.util.HashSet;
import java.util.Set;

public class Intersection {
    public static void main(String[] args) {
        int[] nums1={1,2,2,1};
        int[] nums2={2,2};
        Intersection solution=new Intersection();
        int[] result=solution.intersection(nums1,nums2);
        for(int i:result){
            System.out.println(i);
        }

    }
    public int[] intersection(int[] nums1,int[] nums2){
        Set<Integer> set=new HashSet<Integer>();
        Set<Integer> resSet=new HashSet<Integer>();
        for(int i=0;i<nums1.length;i++){
            set.add(nums1[i]);
        }
        for(int i=0;i<nums2.length;i++){
            if(set.contains(nums2[i])){
                resSet.add(nums2[i]);
            }
        }
        int[] res=new int[resSet.size()];
        int j=0;
        for(int i:resSet){
            res[j]=i;
            j++;
        }
        return res;
    }
}
